using System.ComponentModel;
using EnergyInfo.DAL.LedaTableAdapters;

namespace EnergyInfo.BLL
{
    [DataObject]
    public class DeviceBLL
    {
        #region Adapter

        private DeviceTableAdapter adapter = null;

        protected DeviceTableAdapter Adapter
        {
            get
            {
                if (adapter == null)
                {
                    adapter = new DeviceTableAdapter();
                }

                return adapter;
            }
        }

        #endregion // Adapter

        #region List methods

        /// <summary>
        /// Gets the list of Devices registered in system.
        /// 
        /// </summary>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public DAL.Leda.DeviceDataTable GetDevices()
        {
            return Adapter.GetDevices();
        }

        /// <summary>
        /// Gets all information about device by its identifier.
        /// </summary>
        /// <param name="deviceID"></param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public DAL.Leda.DeviceDataTable GetDeviceByDeviceID(int deviceID)
        {
            return Adapter.GetDeviceByDeviceID(deviceID);
        }

        /// <summary>
        /// Gets all devices belong to object specified by its identifier.
        /// </summary>
        /// <param name="objectID"></param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public DAL.Leda.DeviceDataTable GetDevicesByObjectID(int objectID)
        {
            return Adapter.GetDevicesByObjectID(objectID);
        }

        #endregion // List methods

        #region Add Methods

        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public bool AddDevice()
        {
            // Create a new ProductRow instance
            DAL.Leda.DeviceDataTable Devices = new DAL.Leda.DeviceDataTable();
            DAL.Leda.DeviceRow DeviceRow = Devices.NewDeviceRow();

            FillRow(DeviceRow);

            // Add the new object
            Devices.AddDeviceRow(DeviceRow);
//            int rowsAffected = Adapter..Update(Devices);

            // Return true if precisely one row was inserted,
            // otherwise false
            return true;
        }

        #endregion // Add Methods

        #region Update Functions

        //[DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        //public bool UpdateObject(int deviceID)
        //{
        //    DAL.Leda.DeviceDataTable Devices = Adapter.GetDeviceByDeviceID(deviceID);
        //    if (Devices.Count == 0)
        //    {
        //        // no matching record found, return false
        //        return false;
        //    }

        //    DAL.Leda.DeviceRow deviceRow = Devices[0];

        //    FillRow(deviceRow);

        //    // Update the product record
        //    int rowsAffected = Adapter.Update(deviceRow);

        //    // Return true if precisely one row was updated,
        //    // otherwise false
        //    return rowsAffected == 1;
        //}

        #endregion /// Update Functions

        #region Delete Functions

        /// <summary>
        /// Deletes specified object from database.
        /// </summary>
        /// <param name="deviceID"></param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteDevice(int deviceID)
        {
            int rowsAffected = Adapter.DeleteDevice(deviceID);

            // Return true if precisely one row was deleted,
            // otherwise false
            return rowsAffected == 1;
        }

        #endregion // Delete Functions

        #region Helpers

        private void FillRow(DAL.Leda.DeviceRow row)
        {
        }

        #endregion // Helpers
    }
}